package com.moduls.interfaceFunction.controller;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.Calendar;

import org.apache.commons.io.FilenameUtils;
import org.apache.commons.io.IOUtils;
import org.springframework.web.multipart.MultipartFile;
public class FileUpload {
	/**
     * 文件上传处理
     *
     * @param file
     * @return
     */
    public static String writeUploadFile(MultipartFile file,String module) {
        String filename = file.getOriginalFilename();
        Calendar now = Calendar.getInstance();
        String time= "app/"+now.get(Calendar.YEAR)+"/"
				+(now.get(Calendar.MONTH) + 1)+"/"+now.get(Calendar.DAY_OF_MONTH)+"/";
        String realpath = "D:/wx_fileimage/jkkc/" + time;
        File fileDir = new File(realpath);
        if (!fileDir.exists())
            fileDir.mkdirs();
 
        String extname = FilenameUtils.getExtension(filename);
        String allowImgFormat = "gif,jpg,jpeg,png,xls,xlsx";
        if (!allowImgFormat.contains(extname.toLowerCase())) {
            return "NOT_IMAGE OR EXCEL";
        }
         
        filename = java.util.UUID.randomUUID().toString() + "." + extname;
        InputStream input = null;
        FileOutputStream fos = null;
        try {
            input = file.getInputStream();
            fos = new FileOutputStream(realpath + "/" + filename);
            IOUtils.copy(input, fos);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        } finally {
            IOUtils.closeQuietly(input);
            IOUtils.closeQuietly(fos);
        }
        return time + filename;
    }
}
